package utilities.event;

import hibernatesrc.billStream.ConcreteBill;
import hibernatesrc.billStream.Service;
import hibernatesrc.billStream.ServiceRec;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;

import utilities.Updatable;
import utilities.utilities_components.Duration;
import utilities.utilities_components.IncomeTypeController;

public class ILListener implements EventListener{
	private Duration duration;
	private IncomeTypeController incomeType;
	private Updatable updateItem;
	
	public ILListener( Duration duration, IncomeTypeController incomeType, Updatable updateItem ){
		this.duration = duration;
		this.incomeType = incomeType;
		this.updateItem = updateItem;
	}
	
	public void onEvent( Event event ){
		Date begin = duration.getBegin();
		if( begin != null ){
			Date end = duration.getEnd();
			
			updateItem.update( getData( begin, end, incomeType.getSelectedIndex() ) );
		}
	}
	
	//"房号", "费用码", "入账时间", "钟房", "数量", "单价", "金额", "当时房型", "帐单号"
	private List getData( Date begin, Date end, Session session ){
		List bills=getBillByTime(begin,end,ConcreteBill.getClass(),session);
		
		List ret=new LinkedList();
		Iterator itr=bills.iterator();
		ConcreteBill current=null;
		while(itr.hasNext()){
			current(ConcreteBill)=itr.next();
			
			String roomNumber=current.getAssociatedRoom().getRoomNumber();
			String type=current.getAssociatedRoom().getRoomType().getSizeAndFacilities();
			String hourRoom=current.getCountByHour() ? "是" : "否";
			
			Iterator itr2=current.getServiceList().iterator();
			while(itr2.hasNext()){
				ServiceRec rec=(ServiceRec)itr2.next();
				List col=new LinkedList();
				
				col.add(roomNumber);
				col.add(rec.getService().getInner().getName());
				col.add(rec.getTimePay());
				col.add(hourRoom);
				col.add(rec.getPiece());
				col.add(rec.getService().getInner().getPricePerUnit().getFirst().toString());
				col.add(rec.getPrice());
				col.add(type);
				col.add(current.getId());
				
				ret.add(col);
			}
		}
		
		return ret;
	}
}